﻿<common:LayoutAwarePage x:Name="pageRoot"
                        x:Class="RWSBeeldbank.GroupedItemsPage"
                        DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
                        IsTabStop="false"
                        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                        xmlns:local="using:RWSBeeldbank"
                        xmlns:data="using:RWSBeeldbank.Data"
                        xmlns:common="using:RWSBeeldbank.Common"
                        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
                        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
                        mc:Ignorable="d"
                        NavigationCacheMode="Enabled">

    <Page.Resources>

        <!--
            Collection of grouped items displayed by this page, bound to a subset
            of the complete item list because items in groups cannot be virtualized
        -->
        <CollectionViewSource x:Name="groupedItemsViewSource"
                              Source="{Binding Groups}"
                              IsSourceGrouped="true"
                              ItemsPath="TopItems"
                              d:Source="{Binding AllGroups, Source={d:DesignInstance Type=data:SampleDataSource, IsDesignTimeCreatable=True}}" />
    </Page.Resources>

    <!--
        This grid acts as a root panel for the page that defines two rows:
        * Row 0 contains the back button and page title
        * Row 1 contains the rest of the page layout
    -->
    <Grid Style="{StaticResource LayoutRootStyle}">
        <Grid.RowDefinitions>
            <RowDefinition Height="140" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>

        <SemanticZoom Grid.Row="1">
            <SemanticZoom.ZoomedInView>
                <!-- Horizontal scrolling grid used in most view states -->
                <GridView x:Name="itemGridView"
                          AutomationProperties.AutomationId="ItemGridView"
                          AutomationProperties.Name="Grouped Items"
                          Margin="0,-3,0,0"
                          Padding="116,0,40,46"
                          ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}"
                          ItemTemplate="{StaticResource Standard250x250ItemTemplate}"
                          SelectionMode="None"
                          IsItemClickEnabled="True"
                          ItemClick="ItemView_ItemClick">

                    <GridView.ItemsPanel>
                        <ItemsPanelTemplate>
                            <VirtualizingStackPanel Orientation="Horizontal" />
                        </ItemsPanelTemplate>
                    </GridView.ItemsPanel>
                    <GridView.GroupStyle>
                        <GroupStyle>
                            <GroupStyle.HeaderTemplate>
                                <DataTemplate>
                                    <Grid Margin="1,0,0,6">
                                        <Button AutomationProperties.Name="Group Title"
                                                Click="Header_Click"
                                                Style="{StaticResource TextPrimaryButtonStyle}">
                                            <StackPanel Orientation="Horizontal">
                                                <TextBlock Text="{Binding Title}"
                                                           Margin="3,-7,0,10"
                                                           Style="{StaticResource GroupHeaderTextStyle}" />
                                                <TextBlock Text=":"
                                                           Margin="0,-7,10,10"
                                                           Style="{StaticResource GroupHeaderTextStyle}" />
                                                <TextBlock Text="{Binding MediaObjectsCount}"
                                                           Margin="3,-7,10,10"
                                                           Style="{StaticResource GroupHeaderTextStyle}" />
                                                <TextBlock Text="{StaticResource ChevronGlyph}"
                                                           FontFamily="Segoe UI Symbol"
                                                           Margin="0,-7,0,10"
                                                           Style="{StaticResource GroupHeaderTextStyle}" />
                                            </StackPanel>
                                        </Button>
                                    </Grid>
                                </DataTemplate>
                            </GroupStyle.HeaderTemplate>
                            <GroupStyle.Panel>
                                <ItemsPanelTemplate>
                                    <VariableSizedWrapGrid Orientation="Vertical"
                                                           Margin="0,0,80,0" />
                                </ItemsPanelTemplate>
                            </GroupStyle.Panel>
                        </GroupStyle>
                    </GridView.GroupStyle>
                </GridView>
            </SemanticZoom.ZoomedInView>
            <SemanticZoom.ZoomedOutView>
                <GridView x:Name="groupGridView"
                          Margin="0,-3,0,0"
                          Padding="116,0,40,46">
                    <GridView.ItemTemplate>
                        <DataTemplate>
                            <Grid Margin="0,0,0,0">
                                <Border BorderThickness="1"
                                        BorderBrush="White">
                                    <Image Source="{Binding Group.GroupImage}"
                                           Width="250"
                                           Height="500"
                                           Stretch="Fill" />
                                </Border>
                                <StackPanel VerticalAlignment="Top"
                                            Height="80"
                                            Width="250"
                                            Margin="1 1 1 0"
                                            Background="{StaticResource ListViewItemOverlayBackgroundThemeBrush}">
                                    <TextBlock Text="{Binding Group.Title}"
                                               Foreground="{StaticResource ListViewItemOverlayForegroundThemeBrush}"
                                               Style="{StaticResource TitleTextStyle}"
                                               FontSize="28"
                                               Margin="12"
                                               LineHeight="28" />
                                </StackPanel>
                                <StackPanel VerticalAlignment="Bottom"
                                            Height="60"
                                            Width="250"
                                            Margin="1 0 1 1"
                                            Background="{StaticResource ListViewItemOverlayBackgroundThemeBrush}">
                                    <TextBlock Text="{Binding Group.MediaObjectsCount}"
                                               Foreground="{StaticResource ApplicationSecondaryForegroundThemeBrush}"
                                               Style="{StaticResource TitleTextStyle}"
                                               FontSize="56"
                                               Margin="12,28,12,0"
                                               HorizontalAlignment="Right" />
                                </StackPanel>
                            </Grid>
                        </DataTemplate>
                    </GridView.ItemTemplate>
                </GridView>
            </SemanticZoom.ZoomedOutView>
        </SemanticZoom>

        <!-- Vertical scrolling list only used when snapped -->
        <ListView x:Name="itemListView"
                  AutomationProperties.AutomationId="ItemListView"
                  AutomationProperties.Name="Grouped Items"
                  Grid.Row="1"
                  Visibility="Collapsed"
                  Margin="0,-10,0,0"
                  Padding="10,0,0,60"
                  ItemsSource="{Binding Source={StaticResource groupedItemsViewSource}}"
                  ItemTemplate="{StaticResource Standard80ItemTemplate}"
                  SelectionMode="None"
                  IsItemClickEnabled="True"
                  ItemClick="ItemView_ItemClick">

            <ListView.GroupStyle>
                <GroupStyle>
                    <GroupStyle.HeaderTemplate>
                        <DataTemplate>
                            <Grid Margin="7,7,0,0">
                                <Button AutomationProperties.Name="Group Title"
                                        Content="{Binding Title}"
                                        Click="Header_Click"
                                        Style="{StaticResource TextButtonStyle}" />
                            </Grid>
                        </DataTemplate>
                    </GroupStyle.HeaderTemplate>
                </GroupStyle>
            </ListView.GroupStyle>
        </ListView>

        <!-- Back button and page title -->
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition Width="*" />
            </Grid.ColumnDefinitions>
            <Button x:Name="backButton"
                    Click="GoBack"
                    IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}"
                    Style="{StaticResource BackButtonStyle}" />
            <TextBlock x:Name="pageTitle" Text="{StaticResource AppName}"
                       Grid.Column="1"
                       Style="{StaticResource PageHeaderTextStyle}" />
        </Grid>

        <VisualStateManager.VisualStateGroups>

            <!-- Visual states reflect the application's view state -->
            <VisualStateGroup x:Name="ApplicationViewStates">
                <VisualState x:Name="FullScreenLandscape" />
                <VisualState x:Name="Filled" />

                <!-- The entire page respects the narrower 100-pixel margin convention for portrait -->
                <VisualState x:Name="FullScreenPortrait">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton"
                                                       Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{StaticResource PortraitBackButtonStyle}" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="itemGridView"
                                                       Storyboard.TargetProperty="Padding">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="96,0,10,56" />
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>

                <!--
                    The back button and title have different styles when snapped, and the list representation is substituted
                    for the grid displayed in all other view states
                -->
                <VisualState x:Name="Snapped">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton"
                                                       Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{StaticResource SnappedBackButtonStyle}" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="pageTitle"
                                                       Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="{StaticResource SnappedPageHeaderTextStyle}" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="itemListView"
                                                       Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="Visible" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="itemGridView"
                                                       Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="Collapsed" />
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="groupGridView"
                                                       Storyboard.TargetProperty="Visibility">
                            <DiscreteObjectKeyFrame KeyTime="0"
                                                    Value="Collapsed" />
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Grid>
</common:LayoutAwarePage>
